﻿using System.IO;
using System.Text;
using SoarDEditor.Pandora;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

public class SDUIUtility
{

    /// <summary>
    /// 获得tpSprite
    /// </summary>
    /// <param name="spriteName"></param>
    /// <param name="mat"></param>
    /// <returns></returns>
    public static Sprite FindTPSprite(string spriteName, out Material mat)
    {
        Sprite sp = FindTPSpriteByPath(spriteName, PdrConfig.Instance.UIAtlasPath, out mat);
        if (sp)
            return sp;
        mat = null;
        return null;
    }

    public static Sprite FindTPSpriteByPath(string spriteName, string path, out Material mat)
    {
        if (!Directory.Exists(path))
        {
            mat = null;
            return null;
        }

        var files = Directory.GetFiles(path);
        foreach (var fileName in files)
        {
            if (!fileName.EndsWith(".png")) continue;
            var sprites = AssetDatabase.LoadAllAssetsAtPath(fileName);
            foreach (var sprite in sprites)
            {
                if (!sprite.name.Equals(spriteName)) continue;
                string textureName = fileName.Split('.')[0];
                string atlasName = textureName.Substring(0, textureName.Length - 1) + "mat.mat";
                mat = AssetDatabase.LoadAssetAtPath<Material>(atlasName);
                return sprite as Sprite;
            }
        }

        mat = null;
        return null;
    }

    public static bool SetSDImageSprite(SDImage img, string sourceName = "")
    {
        string spriteName = img.SpriteName;
        Material mat;
        Sprite sp = FindTPSprite(spriteName, out mat);
        if (sp == null)
        {
            Debuger.LogError(string.Format("Sprite找不到:{0} Prefab:{1} Item:{2}", spriteName, sourceName, img.name));
            return false;
        }
        img.sprite = sp;
        img.material = mat;
        img.SpriteName = sp.name;
        return true;
    }

    public static bool FixSpriteMiss(GameObject source)
    {
        bool isRepleace = false;
        SDImage[] images = source.GetComponentsInChildren<SDImage>(true);
        foreach (var img in images)
        {
            if (img.UseSpritePacker) continue;
            if (img.IsEmptyImage) continue;
//            if (img.IsMissing)
//            {
//                string spriteName = null;
//                if (img.sprite != null)
//                {
//                    spriteName = img.sprite.name;
//                }
//                else if (!string.IsNullOrEmpty(img.SpriteName))
//                {
//                    spriteName = img.SpriteName;
//                }
//                if (string.IsNullOrEmpty(spriteName))
//                {
//                    Debug.LogError(string.Format("[Error] Prefab:{0} Item:{1}", source.name, img.name));
//                }
//                SetSDImageSprite(img, source.name);
//            }
            if (!string.IsNullOrEmpty(img.SpriteName) && (img.sprite == null))
            {
                Material mat;
                Sprite sp = FindTPSprite(img.SpriteName, out mat);
                if (sp == null)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append("[prefab]:")
                        .Append(source.name)
                        .Append(" [Item]:")
                        .Append(img.name)
                        .Append("[sprite]:")
                        .Append(img.SpriteName);
                    Debuger.LogError(sb.ToString());
                }
                else
                {
                    img.sprite = sp;
                    img.material = mat;
                    img.SpriteName = sp.name;
                    isRepleace = true;
                }
            }
        }
        return isRepleace;
    }

    public static bool SDImageToUseTexturePacker(GameObject source)
    {
        bool isRepleace = false;
        SDImage[] images = source.GetComponentsInChildren<SDImage>(true);
        foreach (var img in images)
        {
            if (img.UseSpritePacker && img.sprite)
            {
                Material mat;
                Sprite sp = FindTPSprite(img.sprite.name, out mat);
                if (sp == null)
                {
                    Debuger.LogError("该Sprite可能未被打入图集请检查!!!");
                    return isRepleace;
                }
                img.UseSpritePacker = false;
                if (SetSDImageSprite(img, source.name))
                {
                    isRepleace = true;
                }
            }
        }
        return isRepleace;
    }
}
